<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author PR
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <!-- <a-button type="primary" @click="handleEdit({})" v-auth="'wh:pd:whPdStockBill:edit'">
          <Icon icon="i-fluent:edit-12-filled" /> {{ t('批量修改') }}
        </a-button> -->
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ djno: record.djno.djno })">
          {{ record.djno.djno }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <Modal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsWmsWhPdWhPdStockBillListDetail">
  import { unref, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { whPdStockBillDelete, whPdStockBillsListData } from '/@/api/wms/wh/pd/whPdStockBill';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import Modal from './editModal.vue';
  import { useModal } from '/@/components/Modal';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { companyTreeData } from '/@/api/sys/company';
  import { useUserStore } from '/@/store/modules/user';

  const { t } = useI18n('wh.pd.whPdStockBill');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const userStore = useUserStore();

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('盘点单明细管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      // {
      //   label: t('来源id'),
      //   field: 'sourceId',
      //   component: 'Input',
      // },
      {
        label: t('公司'),
        field: 'companyCode',
        fieldLabel: 'companyName',
        component: 'TreeSelect',
        componentProps: {
          api: companyTreeData,
          allowClear: true,
          immediate: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('来源单号'),
        field: 'sourceNo',
        component: 'Input',
      },
      {
        label: t('盘点方式'),
        field: 'djno.pdType',
        component: 'Select',
        componentProps: {
          dictType: 'wms_pd_type',
          allowClear: true,
        },
      },
      {
        label: t('单据编号'),
        field: 'djno.djno',
        component: 'Input',
      },
      // {
      //   label: t('单据状态'),
      //   field: 'djstatus',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'wms_pd_status',
      //     allowClear: true,
      //   },
      // },
      {
        label: t('制单人'),
        field: 'createBy',
        component: 'Input',
      },
      {
        label: t('备注信息'),
        field: 'remarks',
        component: 'Input',
      },
      {
        label: t('单据日期'),
        field: 'djno.ddate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          // showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('盘点状态'),
        field: 'djno.pdStatus',
        component: 'Select',
        componentProps: {
          dictType: 'wms_pd_pdStatus',
          allowClear: true,
        },
      },
      {
        label: t('物料名称'),
        field: 'basInv.invName',
        component: 'Input',
      },
      {
        label: t('物料编码'),
        field: 'basInv.viewCode',
        component: 'Input',
      },
      // {
      //   label: t('总数'),
      //   field: 'iqty',
      //   component: 'Input',
      // },
      // {
      //   label: t('实盘总数'),
      //   field: 'sumqty',
      //   component: 'Input',
      // },
      // {
      //   label: t('仓库'),
      //   field: 'djno.whcode',
      //   component: 'Input',
      // },
      {
        label: t('货位'),
        field: 'basPosition.posName',
        component: 'Input',
      },
      // {
      //   label: t('公司编码'),
      //   field: 'djno.companyCode',
      //   component: 'Input',
      // },
      // {
      //   label: t('公司名称'),
      //   field: 'djno.companyname',
      //   component: 'Input',
      // },

      {
        label: t('盘盈亏状态'),
        field: 'pykStatus',
        component: 'Select',
        componentProps: {
          dictType: 'wms_pd_yk_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('单据状态'),
        field: 'djno.status',
        component: 'Select',
        componentProps: {
          dictType: 'wms_pd_zf_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('备注'),
        field: 'djno.remarks',
        component: 'Input',
      },
    ],
    fieldMapToTime: [['djno.ddate', ['djno.ddate_gte', 'djno.ddate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    // {
    //   title: t('公司编码'),
    //   dataIndex: 'djno.companyCode',
    //   key: 'djno.company_code',
    //   sorter: true,
    //   width: 100,
    //   align: 'left',
    //   show: false,
    // },
    {
      title: t('公司名称'),
      dataIndex: 'djno.companyname',
      key: 'djno.companyname',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('单据编号'),
      dataIndex: 'djno.djno',
      key: 'djno.djno',
      sorter: true,
      width: 100,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('来源单号'),
      dataIndex: 'djno.sourceNo',
      key: 'djno.source_no',
      sorter: true,
      width: 130,
      align: 'left',
      // 
    },
    {
      title: t('盘点方式'),
      dataIndex: 'djno.pdType',
      key: 'djno.pd_type',
      sorter: true,
      width: 130,
      align: 'left',
      dictType: 'wms_pd_type',
    },
    {
      title: t('单据日期'),
      dataIndex: 'djno.ddate',
      key: 'djno.ddate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('盘点状态'),
      dataIndex: 'djno.pdStatus',
      key: 'djno.pd_status',
      sorter: true,
      width: 100,
      align: 'left',
      dictType: 'wms_pd_pdStatus',
    },
    {
      title: t('状态'),
      dataIndex: 'djno.status',
      key: 'djno.status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'wms_pd_zf_status',
    },
    {
      title: t('账存数'),
      dataIndex: 'iqty',
      width: 80,
      key: 'a.iqty',
      sorter: true,
      align: 'right',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 16,
      },
      editRule: false,
      showSummary: true,
      showTotalSummary: true,
    },
    {
      title: t('实盘数'),
      dataIndex: 'frealqty',
      width: 80,
      key: 'a.frealqty',
      sorter: true,
      align: 'right',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 16,
      },
      editRule: false,
      showSummary: true,
      showTotalSummary: true,
    },
    {
      title: t('盈亏数'),
      dataIndex: 'fqty',
      key: 'a.fqty',
      sorter: true,
      width: 100,
      align: 'right',
      showSummary: true,
      showTotalSummary: true,
    },
    {
      title: t('盘盈亏状态'),
      dataIndex: 'pykStatus',
      // key: 'a.pyKstatus',
      dictType: 'wms_pd_yk_status',
      width: 120,
      align: 'left',
    },
    // {
    //   title: t('实盘总数'),
    //   dataIndex: 'djno.sumqty',
    //   key: 'djno.sumqty',
    //   sorter: true,
    //   width: 100,
    //   align: 'right',
    // },
    // {
    //   title: t('仓库'),
    //   dataIndex: 'djno.whcode',
    //   key: 'djno.whcode',
    //   sorter: true,
    //   width: 80,
    //   align: 'left',
    // },
    {
      title: t('货位'),
      dataIndex: 'basPosition.posName',
      key: 'position.pos_name',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('物料编码'),
      dataIndex: 'basInv.viewCode',
      key: 'basInv.view_code',
      sorter: true,
      width: 130,
      align: 'left',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 64,
      },
      editRule: false,
    },
    {
      title: t('物料名称'),
      dataIndex: 'basInv.invName',
      key: 'basInv.inv_name',
      sorter: true,
      width: 230,
      align: 'left',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 64,
      },
      editRule: false,
    },
    {
      title: t('形象刊'),
      dataIndex: 'freeVO.cfree1',
      key: 'a.f109',
      // sorter: true,
      width: 130,
      align: 'left',
      editRow: true,
      editComponent: 'Input',
      editComponentProps: {
        maxlength: 255,
      },
      editRule: false,
    },
    {
      title: t('修改时间'),
      dataIndex: 'djno.updateDate',
      key: 'djno.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注'),
      dataIndex: 'djno.remarks',
      key: 'djno.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },

    // {
    //   title: t('来源id'),
    //   dataIndex: 'sourceId',
    //   width: 130,
    //   align: 'left',
    //   editRow: true,
    //   editComponent: 'Input',
    //   editComponentProps: {
    //     maxlength: 64,
    //   },
    //   editRule: false,
    // },
    // {
    //   title: t('来源明细ID'),
    //   dataIndex: 'a.sourceDid',
    //   key: 'a.source_did',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    //   editRow: true,
    //   editComponent: 'Input',
    //   editComponentProps: {
    //     maxlength: 64,
    //   },
    //   editRule: false,
    // },
    {
      title: t('行备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
      editRow: true,
      editComponent: 'InputTextArea',
      editComponentProps: {
        maxlength: 255,
      },
      editRule: false,
    },
    {
      title: t('盘点人'),
      dataIndex: 'pdByName',
      width: 130,
      align: 'left',
    },
    {
      title: t('盘点时间'),
      dataIndex: 'pdDate',
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑wh_pd_stock_bill'),
        onClick: handleForm.bind(this, { djno: record.djno }),
        auth: 'wh:pd:whPdStockBill:edit',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除wh_pd_stock_bill'),
        popConfirm: {
          title: t('是否确认删除wh_pd_stock_bill'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'wh:pd:whPdStockBill:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload, getSelectRows, getSelectRowKeys, getForm, setProps }] = useTable({
    api: whPdStockBillsListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    showCustomSummary: true,
    showTotalCustomSummary: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { djno: record.djno };
    const res = await whPdStockBillDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  /**
   * 处理成功的回调函数
   *
   * @param record 记录对象
   */
  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  function handleEdit() {
    const selectedRows = getSelectRows();      // 获取选中的行数据
    const selectedKeys = getSelectRowKeys();   // 获取选中的行键值
    if (selectedRows.length === 0) {
      showMessage('请先选择要编辑的数据', 'warning');
      return;
    }

    // 构造传递给 Modal 的数据
    const modalData = {
      selectedRows,
      selectedKeys,
      title: `批量编辑 (${selectedRows.length} 条记录)`
    };

    openModal(true, modalData);
  }
  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/wh/pd/whPdStockBill/exportSubData',
      params: getForm().getFieldsValue(),
    });
  }

  async function handleCompany() {
    await getForm().setFieldsValue({
      companyCode: userStore.getProjecte.code,
      'djno.status': '0',
    });
    reload();
    await getForm().setFieldsValue({
      companyCode: userStore.getProjecte.code,
      'djno.status': '0',
    });
  }
  onMounted(async () => {
    setProps();
    handleCompany();
  });
</script>
